package com.cloud.iotdb.utils.wrapper;

/**
 * @Author: JohnCoding
 * @Description:
 * @date: 2020/3/25 11:42
 */
public class IotQueryWrapper extends BaseIotWrapper {


    public IotQueryWrapper(String url) {
        this.url = url;
    }

    public IotQueryWrapper() {
    }

    @Override
    public BaseIotWrapper le(String timeseriesName, Object val) {
        where.append(timeseriesName + " <= " + val);
        return this;
    }

    @Override
    public BaseIotWrapper eq(String timeseriesName, Object val) {
        where.append(timeseriesName + " = " + val);
        return this;
    }

    public BaseIotWrapper limit(Integer pages) {
        this.sql.append(" limit ").append(pages);
        return this;
    }

    @Override
    public BaseIotWrapper ne(String timeseriesName, Object val) {
        return null;
    }

    @Override
    public BaseIotWrapper gt(String timeseriesName, Object val) {
        where.append(timeseriesName + " > " + val);
        return this;
    }

    @Override
    public BaseIotWrapper ge(String timeseriesName, Object val) {
        where.append(timeseriesName + " >= " + val);
        return this;
    }

    @Override
    public BaseIotWrapper lt(String timeseriesName, Object val) {
        where.append(timeseriesName + " < " + val);
        return this;
    }

    @Override
    public BaseIotWrapper last() {
        this.islast = true;
        return this;
    }

    @Override
    public BaseIotWrapper select(String... timeseries) {
        for (String timesery : timeseries) {
            this.timeseries.add(timesery);
        }
        return this;
    }

    @Override
    public BaseIotWrapper or() {
        this.where.append(" or ");
        return this;
    }

    @Override
    public BaseIotWrapper and() {
        this.where.append(" and ");
        return this;
    }

    @Override
    public StringBuffer getSql() {
        sql.insert(0, "select ");
        if (this.islast){
            sql.append("last ");
        }
        if (null == timeseries || 0 == timeseries.size()) {
            sql.append("* ");
        } else {
            timeseries.forEach(x -> {
                sql.append(x + ",");
            });
        }

        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(" from " + url);
        if (where.length() != 0) {
            sql.append(" where " + where);
        }
        return sql;
    }

    public static void main(String[] args) {
        IotQueryWrapper iotQueryWrapper = new IotQueryWrapper("root.323");
        iotQueryWrapper.last().eq("time",12).and();
        iotQueryWrapper.le("wend",22).and();
        iotQueryWrapper.ge("sidu",22);
        StringBuffer sql = iotQueryWrapper.getSql();
        System.out.println(sql);
    }
}
